﻿/*********************************************************
 * 开发人员：小兵
 * 创建时间：2012-10-9 15:58:09
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
namespace Mozlite.Data.SqlCE
{
    using System;
    using System.Web;
    using System.Data;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;

    /// <summary>
    /// SQLCE数据库解析类。
    /// </summary>
    public class SqlCeParser : SqlParser
    {
        /// <summary>
        /// 初始化类<see cref="SqlCeParser"/>。
        /// </summary>
        public SqlCeParser()
        { }

        /// <summary>
        /// 解析SQL语句，并实行替换。
        /// </summary>
        /// <param name="query">查询语句。</param>
        /// <returns>替换修改后的语句。</returns>
        /// <remarks>
        /// 默认直接返回当前SQL语句，子类重写进行替换。
        /// </remarks>
        public override string Parse(string query)
        {
            return UppercaseIdentifiers(ReplaceIdentifierTokens(query));
        }

        //将SELECT TOP(N)格式转换为LIMIT格式；
        private static readonly Regex selectTopN = new Regex(@"\s*SELECT TOP\s*\(?(\d+)\)?", RegexOptions.Compiled | RegexOptions.Singleline);
        //将IF EXISTS UPDATE INSERT INTO转换为INSERT INTO ON DUPLICATE KEY UPDATE格式；
        private static readonly Regex saveStatement = new Regex(@"^\s*if\s*\(?exists\s*\(?.*?(begin|\r?\n)\s*(update.*?set.*?where.*?)(\r?\n?end|\r?\n)\s*else\s*(begin|\r?\n)\s*(insert into.*?);?(\s*|\r?\n?)(end)?(\s*|\r?\n?)$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline);
        private static readonly Regex updateStatement = new Regex(@"^UPDATE(.*?)SET.*?WHERE(.*)$", RegexOptions.Compiled | RegexOptions.Singleline);

        /// <summary>
        /// 替换关键字，如：GETDATE和NOW等等，所有替换在转换大写后进行。
        /// </summary>
        /// <param name="query">SQL语句。</param>
        /// <returns>返回替换后的字符串。</returns>
        protected override string SyntaxReplace(string query)
        {
            if (string.IsNullOrEmpty(query))
                return string.Empty;
            //将SELECT TOP(N)格式转换为LIMIT格式；
            if (selectTopN.IsMatch(query))
            {
                query = selectTopN.Replace(query, "SELECT TOP($1)");
            }
            //将IF EXISTS UPDATE INSERT INTO转换为INSERT INTO ON DUPLICATE KEY UPDATE格式；
            if (saveStatement.IsMatch(query))
            {
                var match = saveStatement.Match(query);
                var update = match.Groups[2].Value.Trim();
                var insert = match.Groups[5].Value.Trim();
                var checker = updateStatement.Replace(update, "SELECT 1 FROM $1 WHERE$2");
                query = SyntaxSQLCEMarkSave + checker + SyntaxSQLCESep + update + SyntaxSQLCESep + insert;
            }
            return query;
        }

        internal const string SyntaxSQLCEMarkSave = "Syntax.SQLCEMark:Save";
        internal const string SyntaxSQLCESep = "Syntax.SQLCESep:";
    }
}